﻿using Castle.Windsor;
using Castle.Windsor.Installer;

namespace BLL.Installer
{
    /// <summary>
    /// 容器的辅助类，负责容器的初始化、释放、获取容器的操作
    /// 这是一个单例，且该类不允许被继承
    /// </summary>
    public sealed class ContainerHelper
    {
        private static readonly ContainerHelper _instance = new ContainerHelper();

        private ContainerHelper()
        {

        }

        public static ContainerHelper Instance
        {
            get { return _instance; }
        }

        private static IWindsorContainer _container;
        public IWindsorContainer Container
        {
            get { return _container ?? (_container = BootstrapContainer()); }
            private set { _container = value; }
        }

        /// <summary>
        /// 容器的初始化
        /// </summary>
        /// <returns></returns>
        public static IWindsorContainer BootstrapContainer()
        {
            return _container ?? new WindsorContainer().Install(FromAssembly.InThisApplication());
        }

        /// <summary>
        /// 调用该方法以释放容器
        /// </summary>
        public static void DisposeContainer()
        {
            if (_container == null) return;
            _container.Dispose();
        }

    }
}
